package com.newbingo.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.newbingo.dao.imple.IAfficheDAO;
import com.newbingo.entity.Affiche;
import com.newbingo.util.Pager;

/**
 *
 *公告DAO
 * @author ChansonYan
 */

public class AfficheDAO extends HibernateDaoSupport implements IAfficheDAO {
	private static final Log log = LogFactory.getLog(AfficheDAO.class);

	/**
	 * 保存公告
	 */
	public void save(Affiche transientInstance) {
		log.debug("saving Affiche instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* 
	 * 修改公告
	 */
	public void update(Affiche transientInstance) {
		log.debug("saving Affiche instance");
		try {
			getSession().update(transientInstance);
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}
	
	/* 
	 * 删除公告
	 */
	public void delete(Affiche persistentInstance) {
		log.debug("deleting Affiche instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* 
	 * 根据公告ID查询
	 */
	public Affiche findById(final Integer id) {
		log.info("getting Affiche instance with id: " + id);
		try {
			Affiche instance = (Affiche) getSession().get(
					"com.newbingo.entity.Affiche", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* 
	 * 根据实例查询
	 */
	public List findByExample(Affiche instance) {
		log.debug("finding Affiche instance by example");
		try {
			List results = getSession().createCriteria(
					"com.newbingo.entity.Affiche").add(Example.create(instance))
					.list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* 
	 * 
	 */
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Affiche instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Affiche as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* 
	 * 
	 */
	public List findByTitle(Object title) {
		return findByProperty(TITLE, title);
	}

	/* 
	 * 
	 */
	public List findByContents(Object contents) {
		return findByProperty(CONTENTS, contents);
	}
	
	public List findByType(Object type){
		return findByProperty(TYPE, type);
	}

	/* 
	 * 
	 */
	public List findAll() {
		log.debug("finding all Affiche instances");
		try {
			String queryString = "from Affiche";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* 
	 * 
	 */
	public Affiche merge(Affiche detachedInstance) {
		log.debug("merging Affiche instance");
		try {
			Affiche result = (Affiche) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/*
	 * 
	 */
	public void attachDirty(Affiche instance) {
		log.debug("attaching dirty Affiche instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* 
	 *
	 */
	public void attachClean(Affiche instance) {
		log.debug("attaching clean Affiche instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	/**
	 * ָ分页查询方法
	 * @param start
	 * @param limit
	 * @return
	 */
	public Pager findByPage(int start,int limit,Object type){
		Pager pager=new Pager();
		Criteria cri=this.getSession().createCriteria(Affiche.class);
		cri.addOrder(Order.desc("createTime"));
		//判断是否根据公告类型查询
		if(type!=null){
			cri.add(Expression.eq("type",type));
		}
		cri.setProjection(Projections.rowCount());
		pager.setTotalSize(((Number)cri.uniqueResult()).intValue());
		cri.setProjection(null);
		cri.setResultTransformer(Criteria.ROOT_ENTITY);
		cri.setFirstResult(start);
		cri.setMaxResults(limit);
		List afficheList = cri.list();
		pager.setData(afficheList);

		
		return pager;
	}
	
	
}